package com.ywy.controller.admin;

import com.ywy.config.ApplicationInitialize;
import com.ywy.entity.Link;
import com.ywy.service.LinkService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理员友情链接Controller
 *
 * @author ywy
 * @date 2022/9/6 9:00
 */
@Controller
@RequestMapping("admin/link")
public class AdminLinkController {
    @Resource
    private LinkService linkService;

    @Resource
    private ApplicationInitialize initialize;

    /**
     * 跳转到友情链接编辑页面
     *
     * @return
     */
    @RequestMapping({"edit", "edit/{id}"})
    @RequiresPermissions(value = {"LINK_EDIT"})
    public ModelAndView edit(@PathVariable(required = false) Integer id) {
        Link link;
        if (id != null) {
            link = linkService.getById(id);
        } else {
            link = new Link();
        }

        ModelAndView mav = new ModelAndView();
        mav.addObject("link", link);
        mav.setViewName("admin/link/edit");
        return mav;
    }

    /**
     * 分页查询友情链接信息
     *
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    @RequiresPermissions(value = {"LINK_QUERY"})
    public Map<String, Object> list(Integer page, Integer limit) {
        List<Link> linkList = linkService.list(page, limit);
        Long total = linkService.getTotal();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("count", total);
        resultMap.put("data", linkList);
        return resultMap;
    }

    /**
     * 根据id查询友情链接实体
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("getById")
    @RequiresPermissions(value = {"LINK_QUERY"})
    public Map<String, Object> getById(Integer id) {
        Link link = linkService.getById(id);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        resultMap.put("link", link);
        return resultMap;
    }

    /**
     * 添加或者修改类别信息
     *
     * @param link
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("save")
    @RequiresPermissions(value = {"LINK_EDIT"})
    public Map<String, Object> save(Link link, HttpServletRequest request) {
        linkService.save(link);
        initialize.loadData(request.getServletContext());

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }

    /**
     * 删除类别信息
     *
     * @param id
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("delete")
    @RequiresPermissions(value = {"LINK_DELETE"})
    public Map<String, Object> delete(Integer id, HttpServletRequest request) {
        linkService.delete(id);
        initialize.loadData(request.getServletContext());

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("success", true);
        return resultMap;
    }
}
